package com.tyx.modules.manage.controller;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Map;

import com.tyx.common.exception.ServiceException;
import com.tyx.common.validator.ValidatorUtils;
import com.tyx.modules.sys.controller.AbstractController;
import lombok.extern.java.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tyx.modules.manage.entity.PractitionersEntity;
import com.tyx.modules.manage.service.PractitionersService;
import com.tyx.common.utils.PageUtils;
import com.tyx.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author wangchao
 * @email 
 * @date 2019-06-06 09:51:05
 */
@RestController
@RequestMapping("manage/practitioners")
public class PractitionersController extends AbstractController {
    @Autowired
    private PractitionersService practitionersService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("manage:practitioners:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = null;
        try {
            page = practitionersService.queryPage(params,this.getUserId());
            return R.ok().put("page", page);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.error(e.getErrorCode(),e.getErrorMsg());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("manage:practitioners:info")
    public R info(@PathVariable("id") Integer id){
        PractitionersEntity practitioners = practitionersService.getById(id);

        return R.ok().put("practitioners", practitioners);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("manage:practitioners:save")
    public R save(@RequestBody PractitionersEntity practitioners){
        ValidatorUtils.validateEntity(practitioners);
        try {
            practitionersService.saveData(practitioners,this.getUserId());
            return R.ok();
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.error(e.getErrorCode(),e.getErrorMsg());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("manage:practitioners:update")
    public R update(@RequestBody PractitionersEntity practitioners){
        ValidatorUtils.validateEntity(practitioners);
        practitionersService.updateData(practitioners,this.getUserId());
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("manage:practitioners:delete")
    public R delete(@RequestBody Integer[] ids){
        practitionersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 撤销员工状态
     */
    @RequestMapping("/cancel")
    public R cancel(PractitionersEntity practitioners){

        practitionersService.cancel(practitioners);
        return R.ok();
    }
    //导入excel
    @RequestMapping("/batchImport")
    //@RequiresPermissions("manage:practitioners:batchImport")
    @ResponseBody
    public boolean batchImport(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = practitionersService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  a;
    }
}
